package com.gopay.scheduler.shedulerx.util;

import com.gopay.common.cache.client.RedisClient;
import com.gopay.common.constants.scheduler.SchedulerConstants;

import java.util.Map;

/**
 * @author 19371
 */
public class RedisOperator {

    /**
     * 设置简单心跳
     * @param keyHead
     * @param key
     * @param value
     * @param ex
     */
    public static void setSimpleHeartBeat(String keyHead,String key,String value,int ex) throws Exception {
        Long point = RedisClient.hset(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead,
                key,
                Integer.parseInt(value), ex);
        if(point == null){
            throw new Exception();
        }
    }

    /**
     * 设置Url
     * @param keyHead
     * @param key
     * @param value
     */
    public static void setUrlMapping(String keyHead,String key,String value,int ex) throws Exception {
        Long point =RedisClient.hset(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead,
                key,
               value,ex);
        if(point == null){
            throw new Exception();
        }
    }

    /**
     * 设置Job配置
     * @param keyHead
     * @param key
     * @param value
     */
    public static void setJobConfig(String keyHead, String key, String value) throws Exception {
        Long point = RedisClient.hset(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead,
                key,
                value);
        if(point == null){
            throw new Exception();
        }
    }

    /**
     * 获取所有job配置
     * @param keyHead
     * @return
     * @throws Exception
     */
    public static Map<String, String> getAllJobConfig(String keyHead) throws Exception {
        return RedisClient.hgetAll(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead);
    }

    /**
     * 删除job配置
     * @param keyHead
     */
    public static void delJobConfig(String keyHead){
        RedisClient.del(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead);
    }

    /**
     * 删除job操作记录
     * @param key
     */
    public static void delJobOperateCache(String key) throws Exception {
        RedisClient.del(SchedulerConstants.REDIS_PACKAGE_MARK+key);
    }

    /**
     * 设置Job操作记录
     * @param keyHead
     * @param key
     * @param value
     */
    public static void setJobOperateCache(String keyHead, String key, String value) throws Exception {
        Long point = RedisClient.hset(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead,
                key,
                value);
        if(point == null){
            throw new Exception();
        }
    }

    /**
     * 删除所以job操作记录
     * @param keyHead
     * @param key
     */
    public static void delJobOperateCache(String keyHead, String key) throws Exception {
       RedisClient.hdel(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead,key);
    }

    /**
     * 删除param
     * @param keyHead
     * @param key
     */
    public static void delLuckyParam(String keyHead, String key) throws Exception {
       RedisClient.hdel(SchedulerConstants.REDIS_PACKAGE_MARK + keyHead, key);
    }

    /**
     * 添加参数
     * @param keyHead
     * @param key
     * @param value
     */
    public static void setLuckyParam(String keyHead, String key, String value) throws Exception {
        Long point = RedisClient.hset(SchedulerConstants.REDIS_PACKAGE_MARK + keyHead,
                key,
                value);
        if(point == null){
            throw new Exception();
        }
    }

    public static void ping(String keyHead,String key,String value,int ex) throws Exception {
        Long point = RedisClient.hset(SchedulerConstants.REDIS_PACKAGE_MARK+keyHead,
                key,
                value, ex);
        if(point == null){
            throw new Exception();
        }
    }
}
